package com.lemon.utils;

import com.lemon.config.Config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author lenomSeven 1337857379@qq.com
 * @date 2020/10/7
 */
public class DBUtil {
    private final static Logger logger = LoggerFactory.getLogger(DBUtil.class);

    private static ComboPooledDataSource dataSource = null;

    public static void initC3P0ConnectionPool(){
        if (dataSource == null) {
            synchronized (DBUtil.class) {
                try {
                    if (dataSource == null) {
                        dataSource = new ComboPooledDataSource();
                        dataSource.setUser(Config.mysqlUser);
                        dataSource.setPassword(Config.mysqlPwd);
                        dataSource.setJdbcUrl(Config.mysqlUrl);
                        dataSource.setDriverClass(Config.mysqlDriver);
                        dataSource.setMaxPoolSize(Config.maxPool);
                        dataSource.setMinPoolSize(Config.minPool);
                        dataSource.setMaxIdleTime(Config.maxIdleTime);
                    }
                } catch (Exception e) {
                    logger.error("initC3P0ConnectionPool error,", e);
                }
            }
        }
    }

    /**
     * 获取数据库连接
     *
     * @return 数据库连接
     */
    public static Connection getConnection(boolean autoCommit) {
        Connection connection = null;
        while (true) {
            try {
                if (dataSource!=null) {
                    connection = dataSource.getConnection();
                    connection.setAutoCommit(autoCommit);
                } else {
                    logger.error("getConnection error. dataSource is null");
                }
            } catch (SQLException e) {
                logger.error("Get connection from pool error.", e);
            }
            if (connection != null) {
                return connection;
            }
            try {
                logger.info("connector busy.");
                Thread.sleep(1000);
            } catch (Exception e) {
                logger.error("get db Thread sleep connect error", e);
            }
        }
    }

    public static Connection getConnection() {
        return getConnection(false);
    }

    /**
     * 关闭Connection
     */
    public static void closeConnection(Connection conn) {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭Statement
     */
    public static void closeStatement(Statement stat) {
        try {
            if (stat != null)
                stat.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭ResultSet
     */
    public static void closeResultSet(ResultSet rs) {
        try {
            if (rs != null)
                rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接
     */
    public static void close(ResultSet rs, Statement stmt, Connection conn) {
        closeResultSet(rs);
        closeStatement(stmt);
        closeConnection(conn);
    }

}